import 'dart:async';
import 'dart:math';

import 'package:flutter/material.dart';
import 'package:view_lamp/custom/view_lamp_item.dart';

import 'lamp_widget.dart';

///异步随机渐变
///黄云音
///2020年2月26日

class CustomViewLamp2 extends StatefulWidget {
  final double multiColorWidth;
  final double multiColorHeight;

  const CustomViewLamp2({
    Key key,
    this.multiColorWidth,
    this.multiColorHeight,
  }) : super(key: key);

  @override
  _CustomViewLamp2State createState() => _CustomViewLamp2State();
}

class _CustomViewLamp2State extends State<CustomViewLamp2>
    with SingleTickerProviderStateMixin {
  Timer timer;
  List<Color> colorList = [
    Color(0xFFFF0000),
    Color(0xFF00FF00),
    Color(0xFF0000FF),
    Color(0xFFFFFF00),
    Color(0xFF00FFFF),
    Color(0xFFFF00FF),
  ];
  List<ViewLampItem> lampItemInfoList = [
    ViewLampItem(17, 72, Colors.transparent, 'assets/images/previewbulb1-x17y72.png'),
    ViewLampItem(82, 90, Colors.transparent, 'assets/images/previewbulb2-x82y90.png'),
    ViewLampItem(147, 104, Colors.transparent, 'assets/images/previewbulb3-x147y104.png'),
    ViewLampItem(213, 106, Colors.transparent, 'assets/images/previewbulb4-x213y106.png'),
    ViewLampItem(279, 101, Colors.transparent, 'assets/images/previewbulb5-x279y101.png'),
    ViewLampItem(344, 90, Colors.transparent, 'assets/images/previewbulb6-x344y90.png'),
    ViewLampItem(409, 73, Colors.transparent, 'assets/images/previewbulb7-x409y73.png'),
    ViewLampItem(474, 60, Colors.transparent, 'assets/images/previewbulb8-x474y60.png'),
    ViewLampItem(540, 60, Colors.transparent, 'assets/images/previewbulb9-x540y60.png'),
    ViewLampItem(606, 70, Colors.transparent, 'assets/images/previewbulb10-x606y70.png'),
    ViewLampItem(670, 87, Colors.transparent, 'assets/images/previewbulb11-x670y87.png'),
    ViewLampItem(736, 102, Colors.transparent, 'assets/images/previewbulb12-x736y102.png'),
  ];
  AnimationController animationController;
  List<Map<String, Color>> gradientPair = [];

  @override
  void initState() {
    super.initState();
    animationController =
        AnimationController(vsync: this, duration: Duration(seconds: 1));
    animationController.addListener(() {
      setState(() {
        changeItemColor();
      });
    });
    animationController.addStatusListener((status) {
      if (status == AnimationStatus.completed) {
        randomGradientPair(false);
        animationController.reverse();
      } else if (status == AnimationStatus.dismissed) {
        randomGradientPair(true);
        animationController.forward();
      }
    });
    randomGradientPair(true);
    animationController.forward();
  }


  void randomGradientPair(bool isForward) {
    var copyList = List.from(gradientPair);
    for (int i = 0; i < lampItemInfoList.length; i++) {
      int random = Random().nextInt(colorList.length);
      Color beginColor = colorList[random];
      Color endColor = colorList[random];
      if (copyList.isNotEmpty) {
        if (isForward) {
          beginColor = lampItemInfoList[i].color;
        } else {
          endColor = lampItemInfoList[i].color;
        }
      }
      if (beginColor.value == endColor.value) {
        if (random + 1 >= colorList.length) {
          random--;
        } else {
          random++;
        }
        endColor = colorList[random];
      }
      gradientPair.insert(i, {'begin': beginColor, 'end': endColor});
    }
  }

  void changeItemColor() {
    double value = animationController.value;
    for (int i = 0; i < lampItemInfoList.length; i++) {
      var element = gradientPair[i];
      Color begin = element['begin'];
      Color end = element['end'];
      Color valueColor = ColorTween(begin: begin, end: end).lerp(value);
      lampItemInfoList[i].color = valueColor;
    }
  }

  @override
  void dispose() {
    timer?.cancel();
    animationController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return LightChainWidget(lampItemInfoList: lampItemInfoList, multiColorWidth: widget.multiColorWidth,multiColorHeight:widget.multiColorHeight);
  }
}
